<template>
  <el-card class="form-container" shadow="never">
    <el-form
      :model="course"
      :rules="rules"
      ref="courseFrom"
      label-width="150px"
      size="small">
      <el-form-item label="证书图片：" prop="image">
        <single-upload v-model="course.certificate"></single-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('courseFrom')">新增</el-button>
        <el-button @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import SingleUpload from '@/components/Upload/singleUpload'
import { collegeSpeUpdate } from '@/api/college'
import moment from 'moment'

const defaultCourse = {
  'certificate': '',
}
export default {
  name: 'CourseDetail',
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      daterange: [],
      radio: '是',
      course: Object.assign({}, defaultCourse),
      deptOptions: [],
      courseTypeOptions: [],
      priceTypeOptions: [],
      loading: false,
      rules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        certificate: [
          { required: true, message: '请选择封面', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请输入简介', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    setTimeout(() => {
      this.$refs['courseFrom'].resetFields()
    }, 0)
  },
  methods: {
    dateRangeChange (e) {
      this.course.startTime = moment(e[0]).format()
      this.course.endTime = moment(e[1]).format()
      console.log('1', e)
    },
    onSubmit (courseFrom) {
      this.$refs['courseFrom'].validate((valid) => {
        if (valid) {
          this.$confirm('是否提交数据', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // if (this.isEdit) {
            const params = {
              ...this.course,
              id: this.$route.query.id
            }
            collegeSpeUpdate(params).then(response => {
              // this.$refs[formName].resetFields()
              this.$message({
                message: '提交成功',
                type: 'success',
                duration: 1000
              })
              this.$router.back()
            })
          })
        } else {
          this.$message({
            message: '提交失败',
            type: 'error',
            duration: 1000
          })
          return false
        }
      })
    },
    resetForm (courseFrom) {
      this.$refs[courseFrom].resetFields()
      this.course = Object.assign({}, defaultCourse)
    },
    back () {
      this.$router.go(-1)
    }
  }

}
</script>
<style scoped>
  .input-width {
    width: 70%;
  }
</style>
